package com.xfcy.blog.controller.system;



import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xfcy.blog.annotation.OperationLogger;
import com.xfcy.blog.common.R;
import com.xfcy.blog.dto.ArticleDTO;
import com.xfcy.blog.dto.ArticleTopDTO;
import com.xfcy.blog.service.ArticleService;
import com.xfcy.blog.valid.AddGroup;
import com.xfcy.blog.valid.UpdateGroup;
import com.xfcy.blog.vo.ArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 博客文章表(Article)表控制层
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:01:24
 */
@RestController
@RequestMapping("system/article")
@Api(tags = "博客文章管理")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @PostMapping(value = "/list")
    @SaCheckLogin
    @ApiOperation(value = "文章列表", httpMethod = "POST", response = R.class, notes = "文章列表")
    public R list(@RequestBody Map<String,Object> map) {
        return articleService.listArticle(map);
    }

    @OperationLogger(value = "置顶文章或取消置顶")
    @PostMapping(value = "/top")
    @SaCheckPermission("/system/article/top")
    @ApiOperation(value = "置顶文章或取消置顶", httpMethod = "POST", response = R.class, notes = "置顶文章或取消置顶")
    public R top(@RequestBody ArticleTopDTO articleDTO) {
        return articleService.putTopArticle(articleDTO);
    }

    @OperationLogger(value = "上架或下架")
    @PostMapping(value = "/pubOrShelf")
    @SaCheckPermission("/system/article/pubOrShelf")
    @ApiOperation(value = "上架或下架", httpMethod = "POST", response = R.class, notes = "上架或下架")
    public R pubOrShelf(@RequestBody ArticleTopDTO articleDTO) {
        return articleService.pubOrShelf(articleDTO);
    }

    @OperationLogger(value = "删除文章")
    @DeleteMapping("/delete")
    @SaCheckPermission("/system/article/delete")
    @ApiOperation(value = "删除文章", httpMethod = "POST", response = R.class, notes = "删除文章")
    public R delete(Long id) {
        return articleService.delete(id);
    }

    @OperationLogger(value = "批量删除文章")
    @DeleteMapping("/deleteBatch")
    @SaCheckPermission("/system/article/deleteBatch")
    @ApiOperation(value = "批量删除文章", httpMethod = "POST", response = R.class, notes = "批量删除文章")
    public R deleteBatch(List<Long> ids) {
        return articleService.deleteBatch(ids);
    }

    @OperationLogger(value = "添加文章")
    @PostMapping(value = "/add")
    @SaCheckPermission("/system/article/add")
    @ApiOperation(value = "添加文章", httpMethod = "POST", response = R.class, notes = "添加文章")
    public R add(@Validated(AddGroup.class) @RequestBody ArticleDTO articleDTO) {
        return articleService.add(articleDTO);
    }

    @OperationLogger(value = "文章详情")
    @GetMapping(value = "/info")
    @SaCheckPermission("/system/article/info")
    @ApiOperation(value = "文章详情", httpMethod = "GET", response = R.class, notes = "文章详情")
    public R info(Long id) {
        return articleService.info(id);
    }

    @OperationLogger(value = "更新文章")
    @PostMapping(value = "/update")
    @SaCheckPermission("/system/article/update")
    @ApiOperation(value = "更新文章", httpMethod = "POST", response = R.class, notes = "更新文章")
    public R update(@Validated(UpdateGroup.class) @RequestBody ArticleDTO articleDTO) {
        return articleService.updateArticle(articleDTO);
    }

    @OperationLogger(value = "文章爬虫")
    @GetMapping(value = "/reptile")
    @SaCheckPermission("/system/article/reptile")
    @ApiOperation(value = "文章爬虫", httpMethod = "GET", response = R.class, notes = "文章爬虫")
    public R reptile(String url) {
        return articleService.reptile(url);
    }

}

